package cn.wizzer.app.web.modules.controllers.platform.user.account;

import cn.wizzer.app.user.modules.models.UserExchange;
import cn.wizzer.app.user.modules.services.account.UserExchangeService;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.math.BigDecimal;
import java.util.Map;

@IocBean
@At("/platform/user/account/exchange")
public class ExchangeController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserExchangeService exchangeService;

    @At("")
    @Ok("beetl:/platform/user/account/exchange/index.html")
    @RequiresPermissions("user.account.exchange")
    public void index() {

    }

    @At
    @Ok("json:full")
    @RequiresPermissions("user.account.exchange")
    public Object data(@Param("userCnd") String userCnd,
                       @Param("no") String no,
                       String userId,
                       @Param("searchDate") String[] searchDate,
                       String fromCurrencyId, String toCurrencyId,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {

        Long startTime = null;
        Long endTime = null;
        if (null != searchDate && searchDate.length != 0) {
            startTime = Times.d2TS(Times.D(searchDate[0]));
            endTime = Times.d2TS(Times.D(searchDate[1]));
        }
        Pagination pagination = exchangeService.queryVO(userCnd, no, userId, fromCurrencyId, toCurrencyId, pageNumber, pageSize, startTime, endTime);
        // 根据查询条件统计兑换总额
        Map<String, BigDecimal> sum = exchangeService.searchSum(userCnd, no, userId, fromCurrencyId, toCurrencyId, pageNumber, pageSize, startTime, endTime);
        return Result.success().addData(Result.createMap("pagination", pagination, "sum", sum));
    }


    /**
     * @param toUser           用户id
     * @param sourceCurrencyId 源币种
     * @param targetCurrencyId 目标币种
     * @param amount           金额
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.account.exchange")
    public Object exchange(@Param("toUser") String toUser,
                           @ParamVerify(required = true) String sourceCurrencyId,
                           @ParamVerify(required = true) String targetCurrencyId,
                           @ParamVerify(required = true) String amount) {
        // 源币种与目标币种不能相同
        if (sourceCurrencyId.equals(targetCurrencyId)) {
            return Result.error();
        }
        UserExchange exchange = exchangeService.exchange(new BigDecimal(amount), sourceCurrencyId, targetCurrencyId, toUser, "PLATFORM");
        return Result.success().addData(exchange.getId());
    }

}
